﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySearch;
using Kpmg.CompanyInfo;

public partial class Pages_View : System.Web.UI.Page
{
    private IDictionary<string, string> opr;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack) 
        {
            IList<SearchCondition> conditions = (IList<SearchCondition>)Session["SearchContext"];
            if (conditions == null) 
            {
                Response.Redirect("~/user/SearchTicket.aspx");
            }
            string SearchName = Request.QueryString["SearchName"];
            string OwnerStaffNo = Request.QueryString["OwnerStaffNo"];
            tb_searchName.Text = SearchName == null ? "" : SearchName;
            tb_ownerStaffNo.Text = OwnerStaffNo == null ? "" : OwnerStaffNo;

            IList<MyTicket> tickets = SearchOM.SearchTicket(conditions);
            Repeater1.DataSource = tickets;
            Repeater1.DataBind();
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {        
       if (e.Item.ItemType == ListItemType.Item
            || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            MyTicket ticket = e.Item.DataItem as MyTicket;
            ((Label)e.Item.FindControl("Label1")).Text = ticket.Chargeable_Cost_Centre_No;
            ((Label)e.Item.FindControl("Label2")).Text = ticket.Requestor_Staff_No;
            ((Label)e.Item.FindControl("Label3")).Text = ticket.Requestor_Cost_Centre_No;
            ((Label)e.Item.FindControl("Label4")).Text = ticket.Assigned_Team_Code;
        }         
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        string SearchName = tb_searchName.Text;
        string OwnerStaffNo = tb_ownerStaffNo.Text;
        string iid = Utilities.getLoginIID();
        Staff staff = StaffDAO.GetObjectByUserId(iid);
        string staffNo = staff.StaffNo;
        Search search = new Search();
        search.Name = SearchName;
        search.OwnerStaffNo = OwnerStaffNo;
        search.CreatedUser = staffNo;
        search.IsPredefined = false;
        search.CreatedTime = DateTime.Now;
        ((List<SearchCondition>)search.Conditions).AddRange((IList<SearchCondition>)Session["SearchContext"]);
        SearchOM.CreateSearch(search, search.CreatedUser);
        lbl_remind.Text = "Save successfully";
        lbl_remind.Attributes.Add("style", "color:green");
    }
}